﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Lib.CommonForm;

namespace Lib.CommonForm
{
    public class tbl_RoleRight_Info : Standard_Info
    {
        private bool _viewRight, _createRight, _updateRight, _deleteRight, _printRight;

        public bool ViewRight
        {
            get { return _viewRight; }
            set { _viewRight = value; }
        }

        public bool PrintRight
        {
            get { return _printRight; }
            set { _printRight = value; }
        }

        public bool CreateRight
        {
            get { return _createRight; }
            set { _createRight = value; }
        }

        public bool UpdateRight
        {
            get { return _updateRight; }
            set { _updateRight = value; }
        }

        public bool DeleteRight
        {
            get { return _deleteRight; }
            set { _deleteRight = value; }
        }

        private string _roleId;

        public string RoleId
        {
            get { return _roleId; }
            set { _roleId = value; }
        }
        private string _moduleId;

        public string ModuleId
        {
            get { return _moduleId; }
            set { _moduleId = value; }
        }
    }
    public class tbl_RoleRight_Controller
    {
        #region "Contructor"

        private static tbl_RoleRight_Controller _instance;

        protected tbl_RoleRight_Controller() { }

        protected void Dispose()
        {
            _instance = null;
        }

        private static object _syncLock = new object();

        public static tbl_RoleRight_Controller Instance()
        {
            if (_instance == null)
            {
                lock (_syncLock)
                {
                    if (_instance == null)
                        _instance = new tbl_RoleRight_Controller();
                }
            }
            return _instance;
        }

        #endregion

        public const String cs_Table_Name = "tbl_RoleRights";

        public static DataTable dtRoleRight;
        public static string[] roles = null;

        public DataSet GetAll()
        {
            return DataConnect.ExecuteDataset("prc_RoleRights_GetAll");
        }

        public void LoadRoleRight()
        {
            //Get 1 lần cho tất cả các quyền và gán vào biến toàn cục. Các lần Get theo Module sẽ get ngay trên biến toàn cục đó
            //không phải gọi trên Server nữa

            dtRoleRight = tbl_RoleRight_Controller.Instance().GetAll().Tables[0];
            roles = tbl_Roles_Controller.Instance().GetRolesForUser(FormGlobals.strUserName);
        }

        public DataTable GetByRoleName(string RoleName)
        {
            return DataConnect.ExecuteDataset("prc_RoleRights_GetByRoleName", RoleName).Tables[0];
        }
        public bool DeleteByRoleName(string RoleName)
        {
            return (DataConnect.ExecuteNonQuery("prc_RoleRights_DeleteByRoleName", RoleName) > 0);
        }
        public tbl_RoleRight_Info GetInfo(string ModuleID)
        {

            //string[] roles = tbl_Roles_Controller.Instance().GetRolesForUser(FormGlobals.strUserName);

            return GetInfo(roles, ModuleID);
        }

        public tbl_RoleRight_Info GetInfo(string[] roles, string ModuleID)
        {
            tbl_RoleRight_Info retVal = new tbl_RoleRight_Info();

            //DataTable dtRoleRight = tbl_RoleRight_Controller.Instance().GetAll().Tables[0];

            if (dtRoleRight != null)
            {
                DataView dv = dtRoleRight.DefaultView;
                dv.RowFilter = "MODULEID = '" + ModuleID + "'";
                DataTable dt = dv.ToTable();

                if (dt == null)
                    return retVal;

                if (dt.Rows.Count == 0)
                    return retVal;

                retVal.ModuleId = ModuleID;

                foreach (string RoleName in roles)
                {
                    DataRow[] dr1 = dt.Select("LoweredRoleName='" + RoleName.ToLower() + "'");
                    if (dr1 != null)
                    {
                        if (dr1.Length > 0)
                        {
                            if (Convert.ToBoolean(dr1[0]["CREATERIGHT"]))
                                retVal.CreateRight = Convert.ToBoolean(dr1[0]["CREATERIGHT"]);
                            if (Convert.ToBoolean(dr1[0]["UPDATERIGHT"]))
                                retVal.UpdateRight = Convert.ToBoolean(dr1[0]["UPDATERIGHT"]);
                            if (Convert.ToBoolean(dr1[0]["PRINTRIGHT"]))
                                retVal.PrintRight = Convert.ToBoolean(dr1[0]["PRINTRIGHT"]);
                            if (Convert.ToBoolean(dr1[0]["DELETERIGHT"]))
                                retVal.DeleteRight = Convert.ToBoolean(dr1[0]["DELETERIGHT"]);
                            if (Convert.ToBoolean(dr1[0]["VIEWRIGHT"]))
                                retVal.ViewRight = Convert.ToBoolean(dr1[0]["VIEWRIGHT"]);
                        }
                    }
                }
            }
            return retVal;
        }

        public bool Insert(tbl_RoleRight_Info i)
        {
            return (DataConnect.ExecuteNonQuery("prc_RoleRights_Update", i.RoleId, i.ModuleId, i.ViewRight, i.CreateRight, i.UpdateRight, i.PrintRight, i.DeleteRight) > 0);
        }
        public bool Update(tbl_RoleRight_Info i)
        {
            return (DataConnect.ExecuteNonQuery("prc_RoleRights_Update", i.RoleId, i.ModuleId, i.ViewRight, i.CreateRight, i.UpdateRight, i.PrintRight, i.DeleteRight) > 0);
        }
        public bool Append(tbl_RoleRight_Info i)
        {
            return (DataConnect.ExecuteNonQuery("prc_RoleRights_Append", i.RoleId, i.ModuleId, i.ViewRight, i.CreateRight, i.UpdateRight, i.PrintRight, i.DeleteRight) > 0);
        }
        public bool Delete(string RoleID, string ModuleID)
        {
            return (DataConnect.ExecuteNonQuery("prc_RoleRights_Delete", RoleID, ModuleID) > 0);
        }
    }
}
